<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<script>
	function doSearch () {
		$('#drta').datagrid('load',{
	            drugid: $("#drugid").val(),
	            drugname: $("#drugname").val(),
	            drugsal: $("#drugsal").val(),
	            drugeffect: $("#drugeffect").val(),
	            drugplan: $("#drugplan").val()
            });
        $("#sellform").form("reset");
	}
	function addshopp () {
		var obj  = $("#drta").datagrid("getSelections");
		if (obj==null||obj.length<=0) {
			var drugid = $("#drugid").val();
			var drugname = $("#drugname").val();
			var drugsal = $("#drugsal").val();
			if (drugid==null||drugid==""||drugname==null||drugname=="") {
				$.messager.alert('提示','请选择一行或者输入药品ID与名称！');
			} else{
			    $("#addnum").window({
					title:"加入到购物车",
					width:"40%",
				    height:"50%",
				    top:100,
				    modal:true
				});
				$("#addnumform").form("load",{
    			    drugid:drugid,
				    drugname:drugname,
				    drugsal:drugsal
			    });
			    $("#addnum").css("display","block");
			    $("#sellform").form("reset");
			}
		}
		for(i=0;i<obj.length;i++){
		$("#addnum").window({
					title:"加入到购物车",
					width:"40%",
				    height:"50%",
				    top:100,
				    modal:true
				});
				$("#addnum").css("display","block");
				$("#addnumform").form("load",{
    			    drugid:obj[i].drugid,
				    drugname:obj[i].drugname,
				    drugsal:obj[i].drugsal
			    });
				
		}
	}
	
	function computesal () {
		var drugsal3 = $("#drugsal3").val();
		var count3 = $("#count3").val();
		var shoppssall = drugsal3 * count3;
		$("#shoppssal").val(shoppssall);
	}
	//添加到购物车
	function accomplish () {
	    var drugid3 = $("#drugid3").val();
		var drugsal3 = $("#drugsal3").val();
		var count3 = $("#count3").val();
		var shoppssal = $("#shoppssal").val();
		var drugname3 = $("#drugname3").val();
		
		if(count3==0){
		    $.messager.alert('提示','数量不可以为0');
		    return;
		}
		
		$("#drugid2").val(($("#drugid2").val()+","+drugid3.toString()).toString());
		$("#drugname2").val($("#drugname2").val()+","+drugname3);
		$("#count").val($("#count").val().toString()+","+count3.toString());
		var grous = $("#grous").val();
		grous = parseInt(grous) + parseInt(shoppssal);
		$("#grous").val(grous);
		
		$("#addnumform").form("reset");
		$("#addnum").window("close");
		$.messager.alert('提示','成功加入');
	}
	//打开购买药物
	function openbuydrup() {
		$("#shoppdiv").window({
			title: "购物车",
			width: "40%",
			height: "50%",
			top: 100,
			modal: true
		});
		$("#shoppdiv").css("display","block");
	}
	
	//购买药物
	function buydrup() {
		if(!$('#drupbuyform').form('validate')) {
			$.messager.alert('提示', '对不起没有任何药物');
		} else {
			$.post("SellMedicine/sallDrug", $("#drupbuyform").serialize(), function(data) {
				if(data == 200) {
					$.messager.alert('提示', '购买成功');
					$("#drupbuyform").form("reset");
					$("#shoppdiv").window("close");
					$("#drta").datagrid("load");
				} else {
					$.messager.alert('提示', '购买失败');
				}
			});
		}
	}
</script>
<table id="drta" class="easyui-datagrid" data-options="url:'SellMedicine/findDrug',striped:true,fitColumns:true,pagination:true,rownumbers:true,iconCls:'.icon-search',toolbar:'#sellsearch',ctrlSelect:true">
	<thead>
		<th data-options="field:'drugid'">ID</th>
		<th data-options="field:'drugname',resizable:true">药品名称</th>
		<th data-options="field:'drugsize',resizable:true">药品规格</th>
		<th data-options="field:'drugsal',resizable:true">药品售价</th>
		<th data-options="field:'drugeffect',resizable:true">药品用处</th>
		<th data-options="field:'drugplan',resizable:true">是否为处方药</th>
	</thead>
	<tbody>
		
	</tbody>
</table>
<p></p>
&nbsp;&nbsp;&nbsp;
<a href="javascript:void(0)" onclick="addshopp()" class="easyui-linkbutton" data-options="iconCls:'icon-mini-add'">加入</a>
<a href="javascript:void(0)" onclick="openbuydrup()" class="easyui-linkbutton" data-options="iconCls:'icon-mini-add'">购买</a>
<div id="sellsearch" style="padding: 5px;">
	<form id="sellform">
		<label for="drugid">药品ID</label>
		<input type="number" name="drugid" id="drugid" style="width: 100px;" value="" />
		<label for="drugname">药品名称</label>
		<input type="text" name="drugname" id="drugname" value="" />
		<label for="drugsal">药品售价</label>
		<input type="number" name="drugsal" id="drugsal" value="" />
		<label for="drugeffect">药品用处</label>
		<input type="text" name="drugeffect" id="drugeffect" value="" />
		<span>是否为处方药</span>
		<select name="drugplan" id="drugplan">
			<option value="RX" selected>处方药</option>
			<option value="OTC">非处方药</option>
		</select>
		<a href="javascript:void(0)" onclick="doSearch()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
	</form>
</div>
<div id="shoppdiv" style="display: none;">
	<form id="drupbuyform">
		<label for="drugid">药品ID</label>
		<input type="text" name="drugids" id="drugid2" readonly style="width: 100px;" value="" /><br /><br />
		<label for="drugname">药品名称</label>
		<input type="text" name="drugnames" id="drugname2" readonly value="" /><br /><br />
		<label for="count">数量</label>
		<input type="text" name="count" id="count" value="" readonly required /><br /><br />
		<label for="allsal">总价</label>
		<input type="number" name="grous" id="grous" value="0" readonly required /><br /><br />
		<input type="button" onclick="buydrup()" value="完成订单" />
	</form>
</div>
<div id="addnum" style="display: none;">
	<form id="addnumform">
		<input type="hidden" name="drugid" id="drugid3" readonly style="width: 100px;" value="" />
		<label for="drugname">药品名称</label>
		<input type="text" name="drugname" id="drugname3" readonly value="" /><br /><br />
		<label for="drugname">药品单价</label>
		<input type="text" name="drugsal" id="drugsal3" readonly value="" /><br /><br />
		<label for="count">购买数量</label>
		<input type="number" name="count" id="count3" value="0" required onclick="computesal()" /><br /><br />
		<label for="shoppssal">购买价格</label>
		<input type="number" name="shoppssal" id="shoppssal" value="" readonly required /><br /><br />
		<input type="button" onclick="accomplish()" value="确定" />
	</form>
</div>
